package com.zjsd.sqeybusiness.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.zjsd.sqeybusiness.domain.Tcdetail;
import com.zjsd.sqeybusiness.dto.DlaccountDTO;
import com.zjsd.sqeybusiness.mapper.TcdetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zjsd.sqeybusiness.mapper.DlaccountMapper;
import com.zjsd.sqeybusiness.domain.Dlaccount;
import com.zjsd.sqeybusiness.service.IDlaccountService;

/**
 * 渠道代理账户信息Service业务层处理
 *
 * @author Zheng
 * @date 2022-05-10
 */
@Service
public class DlaccountServiceImpl implements IDlaccountService
{
    @Autowired
    private DlaccountMapper dlaccountMapper;

    @Autowired
    private TcdetailMapper tcdetailMapper ;
    @Autowired
    private SysDeptMapper sysDeptMapper ;

    /**
     * 查询渠道代理账户信息
     *
     * @param accountid 渠道代理账户信息ID
     * @return 渠道代理账户信息
     */
    @Override
    public Dlaccount selectDlaccountById(Long accountid)
    {
        return dlaccountMapper.selectDlaccountById(accountid);
    }

    /**
     * 查询渠道代理账户信息列表
     *
     * @param dlaccount 渠道代理账户信息
     * @return 渠道代理账户信息
     */
    @Override
    public List<Dlaccount> selectDlaccountList(Dlaccount dlaccount)
    {
        List<Dlaccount> list = dlaccountMapper.selectDlaccountList(dlaccount);
        if(list.size() > 0 ){
            for(Dlaccount temp : list){
                Tcdetail tcdetail = new Tcdetail();
                tcdetail.setTcid(temp.getTcid());
                List<Tcdetail> tcdetailList = tcdetailMapper.selectTcdetailList(tcdetail);
                temp.setTcdetails(tcdetailList);
            }
        }
        return list;
    }

    /**
     * 新增渠道代理账户信息
     *
     * @param dlaccount 渠道代理账户信息
     * @return 结果
     */
    @Override
    public int insertDlaccount(Dlaccount dlaccount)
    {
        return dlaccountMapper.insertDlaccount(dlaccount);
    }

    /**
     * 修改渠道代理账户信息
     *
     * @param dlaccount 渠道代理账户信息
     * @return 结果
     */
    @Override
    public int updateDlaccount(Dlaccount dlaccount)
    {
        return dlaccountMapper.updateDlaccount(dlaccount);
    }

    /**
     * 批量删除渠道代理账户信息
     *
     * @param accountids 需要删除的渠道代理账户信息ID
     * @return 结果
     */
    @Override
    public int deleteDlaccountByIds(Long[] accountids)
    {
        return dlaccountMapper.deleteDlaccountByIds(accountids);
    }

    /**
     * 删除渠道代理账户信息信息
     *
     * @param accountid 渠道代理账户信息ID
     * @return 结果
     */
    @Override
    public int deleteDlaccountById(Long accountid)
    {
        return dlaccountMapper.deleteDlaccountById(accountid);
    }

    @Override
    public Dlaccount queryZhye(Dlaccount dlaccount) {
        Dlaccount dlaccount1 = dlaccountMapper.selectZhye(dlaccount);
        if(dlaccount1 != null){
            Long deptid = dlaccount.getDeptid();
            if(deptid != null){
                SysDept sysDept = sysDeptMapper.selectDeptById(deptid);
                dlaccount1.setSysdept(sysDept);
            }
            List<DlaccountDTO> list = dlaccountMapper.selectDlaccountListByDeptid(dlaccount);
            dlaccount1.setList(list);
            return dlaccount1;
        }else {
            Dlaccount dlaccount2 = new Dlaccount() ;
            Long deptid = dlaccount.getDeptid();
            if(deptid != null){
                SysDept sysDept = sysDeptMapper.selectDeptById(deptid);
                dlaccount2.setSysdept(sysDept);
            }
            List<DlaccountDTO> list = dlaccountMapper.selectDlaccountListByDeptid(dlaccount);
            dlaccount2.setList(list);
            return  dlaccount2;
        }

    }
}
